using BackData;
using Domain.Warehousies;
using Infratructure;
using MediatR;
using Team.API.Applocation.Command.Warehouse.laneway;

namespace Team.API.Applocation.Hanldler.Warehouse.laneway
{
    /// <summary>
    /// 删除巷道命令处理器
    /// </summary>
    public class DeleteLanewayHandler : IRequestHandler<DeleteLanewayCommand, APIResult<int>>
    {
        private readonly IRepository<Lanewaymodel> _lanewayRep;
        private readonly IRepository<Inventorylocationmodel> _locationRep;
        private readonly ILogger<DeleteLanewayHandler> _logger;

        public DeleteLanewayHandler(
            IRepository<Lanewaymodel> lanewayRep,
            IRepository<Inventorylocationmodel> locationRep,
            ILogger<DeleteLanewayHandler> logger)
        {
            _lanewayRep = lanewayRep;
            _locationRep = locationRep;
            _logger = logger;
        }

        public async Task<APIResult<int>> Handle(DeleteLanewayCommand request, CancellationToken cancellationToken)
        {
            try
            {
                // 查找巷道
                var laneway = await _lanewayRep.GetAll()
                    .Where(x => x.Id == request.Id && !x.IsDel)
                    .FirstAsync();

                if (laneway == null)
                {
                    return new APIResult<int>
                    {
                        Code = StateCode.Fail,
                        Message = "巷道不存在",
                        Result = 0
                    };
                }

                // 检查是否有关联的库位
                var hasLocations = await _locationRep.GetAll()
                    .Where(x => x.Lanewayid == request.Id && !x.IsDel)
                    .AnyAsync();

                if (hasLocations)
                {
                    return new APIResult<int>
                    {
                        Code = StateCode.Fail,
                        Message = "该巷道下存在库位，无法删除",
                        Result = 0
                    };
                }

                // 软删除
                laneway.IsDel = true;
                var result = await _lanewayRep.UpdateAsync(laneway);

                if (result > 0)
                {
                    _logger.LogInformation($"巷道删除成功: {laneway.Lanewayname} (ID={request.Id})");
                    return new APIResult<int>
                    {
                        Code = StateCode.OK,
                        Message = "巷道删除成功",
                        Result = result
                    };
                }
                else
                {
                    return new APIResult<int>
                    {
                        Code = StateCode.Fail,
                        Message = "巷道删除失败",
                        Result = 0
                    };
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "巷道删除失败");
                return new APIResult<int>
                {
                    Code = StateCode.Fail,
                    Message = $"巷道删除失败: {ex.Message}",
                    Result = 0
                };
            }
        }
    }
}
